package xyz.sunne;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Sunne
 * @version 1.0
 * @date 2021/9/27 0027
 */
public class RegexUtil {
    public final static String PHONE_PATTERN = "^(13[0-9]|14[0-9]|15[0-9]|166|17[0-9]|18[0-9]|19[8|9])\\d{8}$";
    public final static String EMAIL_PATTERN = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
    public final static String PASSWORD_PATTERN = "^(\\w){6,18}$";

    public static Boolean isPhone(String phone)  {
        return Pattern.matches(PHONE_PATTERN, phone);
    }

    /**
     * 检测字符串是否全部为NULL以及空字符串
     */
    public static Boolean  notBlankAll(String ...str){
        for (String string : str) {
            if(string==null || "".equals(string.trim())){
                return false;
            }
        }
        return true;
    }


    public static boolean isNotBlank(String str){
        return str != null && !"".equals(str.trim());
    }

    public static boolean pattern(String pattern,String content){
        return Pattern.matches(pattern,content);
    }

    public static String getBookName(String content){
        Pattern pattern = Pattern.compile("《(.*?)》");
        Matcher m = pattern.matcher(content);
        while(m.find()){
            return m.group(1);
        }
        return null;
    }

    //解析GET参数
    public static Map<String, String> getParamParse(String strUrlParam) {
        Map<String, String> mapRequest = new HashMap<>();
        String[] arrSplit ;
        if (strUrlParam == null) {
            return mapRequest;
        }
        arrSplit = strUrlParam.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual;
            arrSplitEqual = strSplit.split("[=]");
            if (arrSplitEqual.length > 1) {
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
            } else {
                if (!arrSplitEqual[0].equals("")) {
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }
}
